home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / eedsrc24.zip / EEP24SRC.ZIP / IGRAPHEP.C < prev    next >
Text File  |  1992-07-29  |  18KB  |  553 lines

  1. /*****************************************************************************
  2. *   General routines to    handle the graphics.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                   Ver 0.1, Oct. 1989    *
  5. *                                         *
  6. * Support: Interface for the virtual device driver.                 *
  7. *****************************************************************************/
  8.  
  9. #define SUPPORT_GIF                 /* Dump it out as GIF file. */
  10.  
  11. #ifdef SUPPORT_GIF
  12. #include "gif_lib.h"
  13. #endif /* SUPPORT_GIF */
  14.  
  15. #include <stdio.h>
  16. #include "virtrstr.h"
  17. #include "program.h"
  18. #include "igraph.h"
  19.  
  20. /* #define DEBUG         Dump virtual screen to Hercules graphic device. */
  21.  
  22. #ifdef __MSDOS__
  23. #include <io.h>
  24. #include <fcntl.h>
  25. #include <bios.h>
  26. #include <dos.h>
  27. #include <conio.h>
  28. #endif /* __MSDOS__ */
  29.  
  30. #define EPSON_SIZE_X 1024
  31. #define EPSON_SIZE_Y 640
  32. #define EPSON_SIZE_MAX 1024            /* The maximum of the two above. */
  33. #define EPSON_SIZE_X2 (EPSON_SIZE_X / 2)
  34. #define EPSON_SIZE_Y2 (EPSON_SIZE_Y / 2)
  35.  
  36. /* The epson specific are defined here:                         */
  37. #define EPSON_WIDTH        80            /* 80 char per line. */
  38. #define EPSON_PIXEL_2_CHAR    8      /* 8 pixels per char, in REG_DENSITY. */
  39.  
  40. #define EPSON_ESC        "\x1b"        /* Actually regular escape char. */
  41. #define EPSON_RESET        "\x1b\x40"     /* Reset the printer - ESC @. */
  42. #define EPSON_VERTICAL_SPACE    "\x1b\x41\x08"   /* 8/72 vert. - ESC A 0x08. */
  43. #define EPSON_REG_DENSITY    "\x1b\x4b"   /* 640 pixels per 7.5" - ESC K. */
  44. #define EPSON_DUAL_DENSITY    "\x1b\x4c"  /* 1280 pixels per 7.5" - ESC L. */
  45.  
  46. static int GraphicMode = FALSE;
  47.  
  48. static void PutString(int DirectPrint, char *Str, int Len);
  49. static void PutString2(int DirectPrint, char *Str, int Len);
  50.  
  51. #ifdef __MSDOS__
  52. #ifdef DEBUG
  53. static void DisplayBufferOnHercules(void);
  54. static void HercGraphics(void);
  55. static void HercPutPoint(int x, int y);
  56. static void HercText(void);
  57. #endif /* DEBUG */
  58. #endif /* __MSDOS__ */
  59.  
  60. /****************************************************************************
  61. * Routine to reset all the system to starting condition    :            *
  62. ****************************************************************************/
  63. void IGInitGraph(void)
  64. {
  65.     if (!VirtInit(EPSON_SIZE_X, EPSON_SIZE_Y)) {
  66.     fprintf(stderr, "Failed to allocate virtual device (no memory).\n");
  67.     MyExit(1);
  68.     }
  69.     GraphicMode = TRUE;
  70. }
  71.  
  72. /****************************************************************************
  73. * Routine to close and shutdown    graphic    mode :                    *
  74. ****************************************************************************/
  75. void IGCloseGraph(void)
  76. {
  77.     if (GraphicMode) {
  78.     VirtClose();
  79.     GraphicMode = FALSE;
  80.     }
  81. }
  82.  
  83. /****************************************************************************
  84. * Routine to dump the screen to the desired output device.            *
  85. ****************************************************************************/
  86. void IGDumpScreen(OutputDriverType OutputKind, int DirectPrint,
  87.                         BooleanType DoubleDensity)
  88. {
  89. #ifdef SUPPORT_GIF
  90.     static GifColorType GifColorMap[] = { { 0, 0, 0 }, { 255, 255, 255 } };
  91.     GifFileType *GifFile;
  92. #endif /* SUPPORT_GIF */
  93.     int i, j, k, Count;
  94.     unsigned int Shift;
  95.     char LineBuffers[8][EPSON_SIZE_Y / 8 + 1], OutputBuffer[EPSON_SIZE_MAX],
  96.     LinePrefixLen[2];
  97.  
  98.     switch (OutputKind) {
  99.     case OUTPUT_EPSON:
  100.         if (DirectPrint != 0) break;
  101.  
  102. #ifdef __MSDOS__
  103.         /* Make sure stdout is output for binary output and buffer it. */
  104.         setmode(1, O_BINARY);
  105. #endif /* __MSDOS__ */
  106.         break;
  107. #ifdef SUPPORT_GIF
  108.     case OUTPUT_GIF:
  109.         if ((GifFile = EGifOpenFileHandle(1)) == NULL) {
  110.         fprintf(stderr, "Failed to open gif file.\n");
  111.         MyExit(-1);
  112.         }
  113.         break;
  114. #endif /* SUPPORT_GIF */
  115.     case OUTPUT_RAW:
  116. #ifdef __MSDOS__
  117.         /* Make sure stdout is output for binary output and buffer it. */
  118.         setmode(1, O_BINARY);
  119. #endif /* __MSDOS__ */
  120.         break;
  121.     default:
  122.         fprintf(stderr, "Output kind is not supported, exit.\n");
  123.         MyExit(-1);
  124.     }
  125.  
  126. #ifdef __MSDOS__
  127. #ifdef DEBUG
  128.     DisplayBufferOnHercules();
  129.     return;
  130. #endif /* DEBUG */
  131. #endif /* __MSDOS__ */
  132.  
  133.     switch (OutputKind) {
  134.     case OUTPUT_EPSON:
  135.         fprintf(stderr, "Dumping line [%d]:     ", EPSON_SIZE_X);
  136.  
  137.         /* Reset printer, and make sure no space between adjacent lines: */
  138.         PutString(DirectPrint, EPSON_RESET, 2);
  139.         PutString(DirectPrint, EPSON_VERTICAL_SPACE, 3);
  140.  
  141.         for (i = 0; i < EPSON_SIZE_X; i += 8) { /* Dump 8 lines at once: */
  142.         fprintf(stderr, "\b\b\b\b%-4d", i);
  143.  
  144.         for (j = 0; j <8; j++)
  145.             VirtGetBlock(i + j, 0, i + j, EPSON_SIZE_Y, LineBuffers[j]);
  146.  
  147.         Shift = 1;
  148.         Count = EPSON_SIZE_Y / 8;
  149.         for (j = 0; j < EPSON_SIZE_Y; j++) {
  150.             OutputBuffer[j] = 0;
  151.             for (k = 0; k < 8; k++) {
  152.             OutputBuffer[j] <<= 1;
  153.             if (LineBuffers[k][Count] & Shift) OutputBuffer[j]++;
  154.             }
  155.             if ((Shift <<= 1) == 256) {
  156.             Shift = 1;
  157.             Count--;
  158.             }
  159.         }
  160.         /* Find last position of non empty pixel byte: */
  161.         for (j = EPSON_SIZE_Y - 1; j >= 0; j--)
  162.             if (OutputBuffer[j] != 0) break;
  163.  
  164.         if (j != 0 || OutputBuffer[0] != 0) {
  165.             j++;
  166.             if (DoubleDensity) {
  167.             PutString(DirectPrint, EPSON_DUAL_DENSITY, 2);
  168.             LinePrefixLen[0] = (j * 2) % 256;
  169.             LinePrefixLen[1] = (j * 2) / 256;
  170.             PutString(DirectPrint, LinePrefixLen, 2);
  171.             PutString2(DirectPrint, OutputBuffer, EPSON_SIZE_Y);
  172.             }
  173.             else {
  174.             PutString(DirectPrint, EPSON_REG_DENSITY, 2);
  175.             LinePrefixLen[0] = j % 256;
  176.             LinePrefixLen[1] = j / 256;
  177.             PutString(DirectPrint, LinePrefixLen, 2);
  178.             PutString(DirectPrint, OutputBuffer, EPSON_SIZE_Y);
  179.             }
  180.         }
  181.         PutString(DirectPrint, "\x0d\x0a", 2);
  182.         }
  183.         break;
  184.     case OUTPUT_RAW:
  185.         fprintf(stderr, "Dumping line [%d]:     ", EPSON_SIZE_X);
  186.  
  187.         for (i = 0; i < EPSON_SIZE_X; i++) {
  188.         fprintf(stderr, "\b\b\b\b%-4d", i);
  189.         VirtGetBlock(i, 0, i, EPSON_SIZE_Y, LineBuffers[0]);
  190.         for (j = 0; j < EPSON_SIZE_Y / 8; j++)
  191.             putchar(LineBuffers[0][j]);
  192.         }
  193.         break;
  194. #ifdef SUPPORT_GIF
  195.     case OUTPUT_GIF:
  196.         fprintf(stderr, "Dumping line [%d]:     ", EPSON_SIZE_Y);
  197.  
  198.         if (EGifPutScreenDesc(GifFile, EPSON_SIZE_X, EPSON_SIZE_Y, 1, 0, 1,
  199.                            GifColorMap) == GIF_ERROR ||
  200.         EGifPutImageDesc(GifFile, 0, 0, EPSON_SIZE_X, EPSON_SIZE_Y,
  201.                         FALSE, 1, NULL) == GIF_ERROR) {
  202.         fprintf(stderr, "Failed to write to gif file.\n");
  203.         MyExit(-1);
  204.         }
  205.  
  206.         for (i = 0; i < EPSON_SIZE_Y; i++) {
  207.         fprintf(stderr, "\b\b\b\b%-4d", i);
  208.         VirtGetBlock(0, i, EPSON_SIZE_X, i, LineBuffers[0]);
  209.         Shift = 1;
  210.         Count = EPSON_SIZE_X / 8;
  211.         for (j = EPSON_SIZE_X; j >= 0; j--) {
  212.             OutputBuffer[j] = (LineBuffers[0][Count] & Shift) != 0;
  213.             if ((Shift <<= 1) == 256) {
  214.             Shift = 1;
  215.             Count--;
  216.             }
  217.         }
  218.         if (EGifPutLine(GifFile, (GifPixelType *) OutputBuffer,
  219.                          EPSON_SIZE_X) == GIF_ERROR) {
  220.             fprintf(stderr, "Failed to write to gif file.\n");
  221.             MyExit(-1);
  222.         }
  223.         }
  224.         if (EGifCloseFile(GifFile) == GIF_ERROR) {
  225.         fprintf(stderr, "Failed to close gif file.\n");
  226.         MyExit(-1);
  227.         }
  228.         break;
  229. #endif /* SUPPORT_GIF */
  230.     }
  231. }
  232.  
  233. /******************************************************************************
  234. * Dumps the string of given length, to Prt. No char in Str has special          *
  235. * meaning, and even zero (NULL) chars are dumped.                  *
  236. * If however DirectPrint is non zero, string is dumped to specifed lpt port.  *
  237. ******************************************************************************/
  238. static void PutString(int DirectPrint, char *Str, int Len)
  239. {
  240.     int i;
  241.  
  242.     if (DirectPrint != 0) {
  243. #ifdef __MSDOS__
  244.     for (i = 0; i < Len; i++) biosprint(0, Str[i], DirectPrint - 1);
  245. #else
  246.     fprintf(stderr, "Output kind is not supported, exit.\n");
  247.     MyExit(-1);
  248. #endif /* __MSDOS__ */
  249.     }
  250.     else
  251.     fwrite(Str, Len, 1, stdout);
  252. }
  253.  
  254. /******************************************************************************
  255. * Dumps the string of given length, to Prt. No char in Str has special          *
  256. * meaning, and even zero (NULL) chars are dumped. Every char is dumped twice. *
  257. * If however DirectPrint is